package com.phonemap.models;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.phonemap.common.Util;

public class User {

	public static Entity createOrUpdateUser(String phone, String firstname,
			String lastname, String email, String password, String avatar) {
		Entity user = getSingleUser(phone);
		if (user == null) {
			user = new Entity("User", phone);
			user.setProperty("phone", phone);
			user.setProperty("firstname", firstname);
			user.setProperty("lastname", lastname);
			user.setProperty("email", email);
			user.setProperty("password", password);
			user.setProperty("avatar", avatar);
			
			user.setProperty("workAndEducation", "");
			user.setProperty("live", "");
			user.setProperty("birthday", "");
			user.setProperty("interestedIn", "");
			user.setProperty("relationshipStatus", "");
			user.setProperty("mobilePhone", "");
			user.setProperty("website", "");
			user.setProperty("introduction", "");
			user.setProperty("status", "1");
			
		} else {
			user.setProperty("phone", phone);
			user.setProperty("firstname", firstname);
			user.setProperty("lastname", lastname);
			user.setProperty("password", password);
			user.setProperty("avatar", avatar);
			
		}
		Util.persistEntity(user);
		return user;
	}
	
	public static Entity insertUser(String id, String name, String first_name, String last_name,
			String link, String birthday, String interested_in,
			String relationship_status, String website, String timezone,
			String locale, String verified, String updated_time, String username) {
		Entity user = getSingleUser(id);
		if (user == null) {
			user = new Entity("User", id);
			user.setProperty("user_id", id);
			user.setProperty("name", name);
			user.setProperty("first_name", first_name);
			user.setProperty("last_name", last_name);
			user.setProperty("link", link);
			user.setProperty("birthday", birthday);
			user.setProperty("interested_in", interested_in);
			user.setProperty("relationship_status", relationship_status);
			user.setProperty("website", website);
			user.setProperty("timezone", timezone);
			user.setProperty("locale", locale);
			user.setProperty("verified", verified);
			user.setProperty("updated_time", updated_time);
			user.setProperty("username", username);
			user.setProperty("user_status", "1");
		}

		return user;
	}
	
	public static Entity updateUser(String phone, String firstname,
			String lastname, String workAndEducation, String live, String birthday,
			String interestedIn, String relationshipStatus, String email,
			String mobilePhone, String website, String introduction) {
		Entity user = getSingleUser(phone);
		if (user != null) {
			user.setProperty("phone", phone);
			user.setProperty("firstname", firstname);
			user.setProperty("lastname", lastname);
			user.setProperty("workAndEducation", workAndEducation);
			user.setProperty("live", live);
			user.setProperty("birthday", birthday);
			user.setProperty("interestedIn", interestedIn);
			user.setProperty("relationshipStatus", relationshipStatus);
			user.setProperty("mobilePhone", mobilePhone);
			user.setProperty("website", website);
			user.setProperty("introduction", introduction);
			user.setProperty("email", email);
			user.setProperty("status", "1");
			Util.persistEntity(user);
		} 
		
		return user;
	}
			

	public static Entity getSingleUser(String phone) {
		Iterable<Entity> results = Util.listEntities("User", "phone", phone);
		List<Entity> entity = new ArrayList<Entity>();
		for (Entity e : results)
			if (e != null)
				entity.add(e);
		if (!entity.isEmpty()) {
			return (Entity) entity.remove(0);
		}
		return null;
	}

	public static Iterable<Entity> getAllUsers() {
		Iterable<Entity> entities = Util.listEntities("User", null, null);
//		List<Entity> userList = new ArrayList<Entity>();
//		for (Entity entity : entities) {
//			userList.add(entity);
//		}
		return entities;
	}

	public static Iterable<Entity> getAllUsers(String kind) {
		Iterable<Entity> entities = Util.listEntities(kind, null, null);
		return entities;
	}

	public static Entity getUser(String phone) {
		Key key = KeyFactory.createKey("User", phone);
		return Util.findEntity(key);
	}

	public static Iterable<Entity> getUsers(String firstname) {
		Iterable<Entity> entities = Util.listEntities("User", "firstname",
				firstname);
		return entities;
	}

	/**
	 * Delete product entity
	 * 
	 * @param productKey
	 *            : product to be deleted
	 * @return status string
	 */
	public static String deleteUser(String productKey) {
		Key key = KeyFactory.createKey("User", productKey);

		List<Entity> items = getPositions(productKey);
		if (!items.isEmpty()) {
			return "Cannot delete, as there are items associated with this product.";
		}
		Util.deleteEntity(key);
		return "User deleted successfully";
	}

	public static List<Entity> getPositions(String name) {
		Query query = new Query();
		Key parentKey = KeyFactory.createKey("User", name);
		query.setAncestor(parentKey);
		query.addFilter(Entity.KEY_RESERVED_PROPERTY,
				Query.FilterOperator.GREATER_THAN, parentKey);
		List<Entity> results = Util.getDatastoreServiceInstance()
				.prepare(query).asList(FetchOptions.Builder.withDefaults());
		return results;
	}
	
	public static Iterable<Entity> getUsers(String searchBy,
			String searchFor) {
		return Util.listEntities("User", searchBy, searchFor);
	}
	
}
